package cn.edu.zafu.bigdata.x.wgj.k;
import java.sql.*;
public class DatabaseManager {
    private Connection connection;

    public DatabaseManager() {
        try {
            String url = "jdbc:mysql://192.168.169.76:3306/learnjava";
            this.connection = DriverManager.getConnection(url, "learn", "learnpassword");
            Statement stmt = connection.createStatement();
            String sql = "CREATE TABLE IF NOT EXISTS Student202220030208 (" +
                    "id VARCHAR(255) PRIMARY KEY," +
                    "name VARCHAR(255)," +
                    "phone VARCHAR(255))";
            stmt.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void addStudent(Student202220030208 student202220030208) throws SQLException {
        String sql = "INSERT INTO Student202220030208 (id, name, phone) VALUES (?, ?, ?)";
        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setString(1, student202220030208.getId());
            pstmt.setString(2, student202220030208.getName());
            pstmt.setString(3, student202220030208.getPhone());
            int affectedRows = pstmt.executeUpdate();
            if (affectedRows > 0) {
                System.out.println("Student added successfully!");
            }
        }
    }
    public Student202220030208 getStudent(String id) throws SQLException {
        String sql = "SELECT * FROM Student202220030208 WHERE id = ?";
        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setString(1, id);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                return new Student202220030208(rs.getString("id"), rs.getString("name"), rs.getString("phone"));
            }
        }
        return null;
    }
    public void updateStudent(Student202220030208 student202220030208) throws SQLException {
        String sql = "UPDATE Student202220030208 SET name = ?, phone = ? WHERE id = ?";
        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setString(1, student202220030208.getName());
            pstmt.setString(2, student202220030208.getPhone());
            pstmt.setString(3, student202220030208.getId());
            int affectedRows = pstmt.executeUpdate();
            if (affectedRows > 0) {
                System.out.println("Student updated successfully!");
            }
        }
    }
    public void deleteStudent(String id) throws SQLException {
        String sql = "DELETE FROM Student202220030208 WHERE id = ?";
        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setString(1, id);
            int affectedRows = pstmt.executeUpdate();
            if (affectedRows > 0) {
                System.out.println("Student deleted successfully!");
            }
        }
    }
    public void closeConnection() throws SQLException {
        if (connection != null) {
            connection.close();
        }
    }
}